// -------------------------------------------------------------
// Title:	HTMLog library
// Version:	0.80
// Description:	Simple class for printing logs in your programs.
//		Supporting printing to html and stdout.
// Dependences:	No
// File:	htmlog.cxx
// License:	GNU Library General Public License 2 (LGPL2)
// Authors:	Evgeny Bronnikov <bbroth@pochta.ru>
// -------------------------------------------------------------


#include "htmlog.h"

HTMLog::HTMLog(){

  log_target = HTM_FILE;
  html_init();
  
}
// -----------------------------------------------

HTMLog::HTMLog(int target){

  log_target = target;
  if (log_target==HTM_FILE) html_init();
 
}
// -----------------------------------------------

HTMLog::~HTMLog(){

  if(log_target==HTM_FILE){
    fputs("\n</BODY>\n</HTML>\n",fp);
    fclose(fp);
  }

}
// -----------------------------------------------

int HTMLog::Plain(char *message){

  switch(log_target){
  case HTM_FILE:
    fputs(message, fp);
    fputs("\n", fp);    
    break;
    
  case HTM_STDOUT:
    printf("%s\n",message);
    break;
    
  case HTM_EMPTY:
    break;
  
  default:
    break;
    
  }  

}
// -----------------------------------------------

int HTMLog::Custom(char *message, int style, char* color){

  switch(log_target){
  case HTM_FILE:
    fputs("<P align=\'left\'>",fp);
    // set styles
    if(style&HTM_STYLE_BOLD){
      fputs("<B>", fp);
    }
    if(style&HTM_STYLE_ITALIC){
      fputs("<I>", fp);
    }
    if(style&HTM_STYLE_UNDERLINE){
      fputs("<U>", fp);
    }
    
    fprintf(fp, "<FONT color=\'#%s\'>%s</FONT>", color,message);
    

    if(style & HTM_STYLE_UNDERLINE){
      fputs("</U>", fp);
    }
    if(style & HTM_STYLE_ITALIC){
      fputs("</I>", fp);
    }
    if(style & HTM_STYLE_BOLD){
      fputs("</B>", fp);
    }

    fputs("</P>\n", fp);
    break;
    
  case HTM_STDOUT:
    printf("%s\n",message);
    break;
    
  case HTM_EMPTY:
    break;
  
  default:
    break;
    
  }  
  

}
// -----------------------------------------------

int HTMLog::Info(char *message){

  switch(log_target){
  case HTM_FILE:
    fprintf(fp, "<P align=\'left\'><FONT color=\'#00AA00\'><B>INFO:</B></FONT> %s</P>\n",message);
    break;
  
  case HTM_STDOUT:
    printf("*INFO: %s\n",message);
    break;
  
  case HTM_EMPTY:
    break;
  
  default:
    break;
    
  }  

}
// -----------------------------------------------

int HTMLog::Warning(char *message){

  switch(log_target){
  case HTM_FILE:
    fprintf(fp, "<P align=\'left\'><FONT color=\'#0000FF\'><B>WARNING:</B></FONT> %s</P>\n",message);    
    break;
  
  case HTM_STDOUT:
    printf("*WARNING: %s\n",message);
    break;
  
  case HTM_EMPTY:
    break;
  
  default:
    break;
    
  }  

}
// -----------------------------------------------

int HTMLog::Error(char *message){

  switch(log_target){
  case HTM_FILE:
    fprintf(fp, "<P align=\'left\'><FONT color=\'#FF0000\'><B>ERROR:</B></FONT> %s</P>\n",message);        
    break;
  
  case HTM_STDOUT:
    printf("*ERROR: %s\n",message);
    break;
  
  case HTM_EMPTY:
    break;
  
  default:
    break;
    
  }  

}
// -----------------------------------------------

int HTMLog::html_init(){

  if((fp=fopen(LogFile,"w"))==NULL){
    printf("Error creating file...\nLog to stdout\n");
    log_target = HTM_STDOUT;
    return -1;
  }else{
    fputs("<HTML><HEAD>\n<TITLE>Log File</TITLE>\n</HEAD>\n<BODY bgcolor=\'#CCCCCC\'>\n",fp);
    fprintf(fp, "<P align=\'center\'><I>This is <B>HTMLog system</B> log file.</I></P>\n");
  }
  
  return 0;
}
// -----------------------------------------------
